package com.neu.flashmall.ms.controller;

import com.neu.common.utils.Result;
import com.neu.flashmall.ms.entity.SysUser;
import com.neu.flashmall.ms.service.SysRoleMenuService;
import com.neu.flashmall.ms.vo.SysRoleMenuVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/sysRoleMenu")
public class SysRoleMenuController {

    @Resource
    private SysRoleMenuService roleMenuService;

    @PutMapping("/check/updateRoleMenu")
    @ApiOperation("修改某个角色的权限")
    public Result updateRoleMenu(@RequestBody SysRoleMenuVo sysRoleMenuVo){
        String result = this.roleMenuService.updateRoleMenu(sysRoleMenuVo.getRoleList(),sysRoleMenuVo.getRoleId());
        if(result.equals("更新权限成功")){
            return Result.ok().setMessage(result);
        }
        return Result.error().setMessage(result);
    }

    @GetMapping("/getAllAuthority")
    @ApiOperation("获取所有的权限,只有超级管理员才可以调用")
    public Result getAllAuthority(){
        String result = this.roleMenuService.getAllAuthority();
        if(result.equals("获取所有权限成功")){
            return Result.ok().setMessage(result);
        }
        return Result.error().setMessage(result);
    }

    @PostMapping(value = "/deleteAllOtherRoleAuthority")
    @ApiOperation("清除所有其他角色的权限，除了超级管理员")
    public Result deleteAllOtherRoleAuthority(@RequestBody SysUser sysUser){
        String result = this.roleMenuService.deleteAllOtherRoleAuthority(sysUser.getUsername(), sysUser.getPassword());
        if(result.equals("删除成功")){
            return Result.ok().setMessage(result);
        }
        return Result.error().setMessage(result);
    }
}
